//
//  HMHomeController.swift
//  weibo10
//
//  Created by mac on 15/10/17.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit
import SVProgressHUD

let statusNormalReuseId = "statusNormalReuseId"
let statusRetweetedReuseId = "statusRetweetedReuseId"
class HMHomeController: HMVisitorController {
    
    /// 懒加载数据列表模型
    private lazy var listViewModel = HMStatusListViewModel()
    // MARK: -懒加载刷新控件
    private lazy var pullView: UIActivityIndicatorView = {
       let indicator = UIActivityIndicatorView(activityIndicatorStyle:UIActivityIndicatorViewStyle.WhiteLarge)
        indicator.color = UIColor.grayColor()
        return indicator
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        if !HMUserAcountViewModel.sharedUserAccount.userLogon {
            visitorView?.setupInfo(nil, labelName: "关注一些人，回这里看看有什么惊喜")
            return
        }
        
        /// 注册可重用cell
        prepareCell()
        loadData()
    }
    
    /// 准备表格
    private func prepareCell(){
        tableView.estimatedRowHeight = 400
        // 去掉分割线
        tableView.separatorStyle = .None
        
        // 下拉刷新： 默认没有
        refreshControl = HMRefreshControl()
        // 添加监听事件
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)
        refreshControl?.tintColor = UIColor.clearColor()
        // 转发
        tableView.registerClass(HMRetweetsdStatusCell.self, forCellReuseIdentifier: statusRetweetedReuseId)
        // 原创
        tableView.registerClass(HMStatusNormalCell.self, forCellReuseIdentifier: statusNormalReuseId)
        // 上拉刷新
        tableView.tableFooterView = activityView
    }

    func loadData(){
        refreshControl?.beginRefreshing()
        listViewModel.loadStatus(isPullup: activityView.isAnimating()) { (isSuccess) -> () in
            // 关闭刷新
            self.refreshControl?.endRefreshing()
            self.activityView.stopAnimating()
            if !isSuccess {
                SVProgressHUD.showErrorWithStatus("网络出现问题，请重新加载")
                return
            }
            self.tableView.reloadData()
        }
    }
    /// 懒加载红下拉刷新控件
    private lazy var activityView: UIActivityIndicatorView = {
        let actV = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        actV.color = UIColor.lightGrayColor()
        return actV
    }()
}
// MARK: - 数据源方法、代理方法
extension HMHomeController{
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let vm = listViewModel.statusList[indexPath.row]
        let cell = tableView.dequeueReusableCellWithIdentifier(vm.cellId, forIndexPath: indexPath) as! HMStatusCell
        if indexPath.row == listViewModel.statusList.count - 1 && !activityView.isAnimating() {
            activityView.startAnimating()
            loadData()
        }
        cell.viewModel = vm
        return cell
    }
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        print(indexPath.row)
        return listViewModel.statusList[indexPath.row].rowHeight
    }
    // 选中
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("didSelected")
    }
}

